﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DTO;

namespace DAO
{

	public class CHUCVUDAO:IData<CHUCVU> 
 	{
		public CHUCVUDAO(String myconnecString) : base(myconnecString) { }

		protected override int InsertOnSubmit(ref CHUCVU item, int t) {
			 SqlParameter p = new SqlParameter(); 
			 if( t == 0)
				p.Direction = ParameterDirection.Output;
			 p.ParameterName = "@" + CHUCVU.c_ID;
			 p.Value = item.ID;
			 Object obj = item.ID; 
			int i = connec.ExecuteNonQuery("sp_CHUCVU", CommandType.StoredProcedure,p,ref obj,new SqlParameter("@flag", t),
				 new SqlParameter("@" + CHUCVU.c_MACV, item.MACV), new SqlParameter("@" + CHUCVU.c_TENCV, item.TENCV));
			if(t == 0)
				item.ID = Convert.ToInt32(obj);
			return i;
		}

		public override CHUCVU GetItem(IDataReader dr)
		{
			CHUCVU obj = new CHUCVU();
			obj.ID = Convert.ToInt32(dr[CHUCVU.c_ID]);
			obj.MACV = Convert.ToString(dr[CHUCVU.c_MACV]);
			obj.TENCV = Convert.ToString(dr[CHUCVU.c_TENCV]);
			return obj;
		}

		public override List<CHUCVU> GetAll()
		{
			return GetAll("sp_GetAllCHUCVU", CommandType.StoredProcedure);
		}

		public override CHUCVU GetItemById(object id)
		{
			return GetAll().SingleOrDefault(p => p.ID == Convert.ToInt32(id));
		}
		public override CHUCVU GetItemById(CHUCVU item){
			throw new NotImplementedException();
		}

	}

 }
